using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;
using XLua;
/*
 UI节点实力搜索工具类
 */
public class UINodeHelper 
{
    public static UnityEngine.Object[] GetUINodes(Transform transform , LuaTable nodes , string type)
    {
        UnityEngine.Object[] components = new UnityEngine.Object[nodes.Length];
        nodes.ForEach<int, string>((k, v) => {
            if(transform.Find(v) != null)
            {
                if (type == "GameObject")
                {
                    components[k - 1] = transform.Find(v).gameObject;
                }
                else if (type == "Image")
                {
                    components[k - 1] = transform.Find(v).GetComponent<Image>();
                }
                else if (type == "Transform")
                {
                    components[k - 1] = transform.Find(v);
                }
                else if (type == "Text")
                {
                    components[k - 1] = transform.Find(v).GetComponent<Text>();
                }
            }
        });
        return components;
    }

    //刷新下拉框显示
    public static void UpdateDropdownView(Dropdown dropdown, LuaTable showNames)
    {
        //清空下下拉框数据
        dropdown.options.Clear();
        Dropdown.OptionData tempData;
        var defultName = "";
        showNames.ForEach<int, string>((k, v) => {
            tempData = new Dropdown.OptionData();
            tempData.text = v;
            dropdown.options.Add(tempData);
            if (defultName == "") defultName = v;
        });
        //把第一条数据显示为默认
        dropdown.captionText.text = defultName;
    }

    public static UnityEngine.Object[] GetNodeChildren(Transform transform)
    {
        var childCount = transform.childCount;
        UnityEngine.Object[] components = new UnityEngine.Object[childCount];
        for (int i = 0; i < childCount; i++)
        {
            var child = transform.GetChild(i);
            components[i] = child;
        }
        return components;
    }

    //立即刷新物体格式大小
    public static void LayoutRebuilderRefre(RectTransform rect)
    {
        LayoutRebuilder.ForceRebuildLayoutImmediate(rect);
    }
}
